﻿using System.Windows.Forms;

namespace Siav
{
    class csControles
    {
        public void TrocaTABporEnter(Control _ctrl)
        {
            if (_ctrl.HasChildren)
            {
                foreach (Control _child in _ctrl.Controls)
                {
                    if (_ctrl.HasChildren)
                        TrocaTABporEnter(_child);
                }
            }
            else
            {
                ///Não funciona para Numeric Up Down
                if (_ctrl is TextBox ||
                _ctrl is MaskedTextBox ||
                _ctrl is ListBox ||
                _ctrl is CheckBox ||
                _ctrl is TabPage ||
                _ctrl is DateTimePicker ||
                _ctrl is ComboBox)
                {
                    /// inibe a ação do Enter para evitar o comportamento de
                    /// Accept em alguns casos
                    _ctrl.KeyDown += delegate(object sender, KeyEventArgs e)
                    {
                        if (e.KeyCode == Keys.Enter)
                            e.SuppressKeyPress = true;
                    };
                    /// adiciona a tratativa à tecla Enter e envia o TAB para
                    /// promover a navegação
                    _ctrl.KeyUp += delegate(object sender, KeyEventArgs e)
                    {
                        if (e.KeyCode == Keys.Enter)
                        {
                            // SendKeys.Send("{TAB}");
                            _ctrl.FindForm().SelectNextControl(_ctrl, true, true, true, false);
                        }
                    };
                }
            }
        }
    }
}
